/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.solarchallenge.client.gwtui.applicants.createapplication;

import static com.google.solarchallenge.client.event.HistoryHandler.createTokenWithParams;
import static com.google.solarchallenge.client.rpc.ServiceProvider.getServiceProvider;
import static com.google.solarchallenge.shared.Utils.RedirectionUtils.generateValidParamsToAttach;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.solarchallenge.client.event.EventCategory;
import com.google.solarchallenge.client.event.HistoryHandler;
import com.google.solarchallenge.client.gwtui.mvpinterfaces.Presenter;
import com.google.solarchallenge.shared.ValidParams;

/**
 * Presenter for CreateApplication UI.
 *
 * @author Arjun Satyapal
 */
public class CreateApplicationPresenter extends Presenter {
  private CreateApplicationDisplay display;

  private String applicationId;


  public CreateApplicationPresenter(CreateApplicationDisplay display,
      String historyToken) {
    super(display.getSolarChallengeHeaderPanel(), historyToken);
    this.display = display;
  }

  @Override
  public void go(HasWidgets container) {
    container.clear();
    container.add(display.asWidget());
    bind();
  }

  @Override
  public void bind() {
    display.getButtonCreateApplication().addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        if (display.getTextBoxApplicationDescription().getText().isEmpty()) {
          Window.alert("Enter some description.");
        } else {
          display.setEnabled(false);
          getServiceProvider().getApplicantService().newApplicationId(
              display.getTextBoxApplicationDescription().getText(),
              new AsyncCallback<String>() {
                @Override
                public void onSuccess(String result) {
                  applicationId = result;
                  String token = createTokenWithParams(
                      EventCategory.APPLICANT_NEW_APPLICATION,
                      generateValidParamsToAttach(ValidParams.APPLICATION_ID,
                          applicationId));
                  HistoryHandler.handleNewToken(token);
                }

                @Override
                public void onFailure(Throwable caught) {
                  display.setEnabled(true);
                  Window.alert("Failed to get new ApplicationId due to ["
                      + caught.getLocalizedMessage() + "].");
                }
              });
        }
      }
    });

    display.getButtonCancel().addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        HistoryHandler.handleNewToken(EventCategory.APPLICANT_HOME.getToken());
      }
    });
  }
}
